﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;
using Token.Util;

namespace Token.Piker.Web.AJAX
{
    /// <summary>
    /// Classify 的摘要说明
    /// </summary>
    public class Classify : IHttpHandler, IRequiresSessionState
    {
        Token.Piker.BLL.PhotoTagBLL singleBll = new Token.Piker.BLL.PhotoTagBLL();
        Token.Piker.BLL.AlbumBLL albumBll = new Token.Piker.BLL.AlbumBLL();
        Token.Piker.BLL.ViewPhotoTagBLL VsingleBll = new Token.Piker.BLL.ViewPhotoTagBLL();
        public static int pageSize = 15;
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public class ImgInfo
        {
            private string _classify;

            public string Classify
            {
                get { return _classify; }
                set { _classify = value; }
            }

            private int _totalNum;

            public int TotalNum
            {
                get { return _totalNum; }
                set { _totalNum = value; }
            }

            private List<Model.Photo> _photo;

            public List<Model.Photo> Photo
            {
                get { return _photo; }
                set { _photo = value; }
            }

            private List<Model.Album> _album;

            public List<Model.Album> Album
            {
                get { return _album; }
                set { _album = value; }
            }

        }

        public void ProcessRequest(HttpContext context)
        {
            object fun_id = context.Request["f_id"];  //读取方法标识
            context.Response.ContentType = "text/plain";
            //大图预加载
            if (fun_id != null && fun_id.ToString() == "BII")
            {
                int paraIndex = context.Request["Index"] == null ? 1 : int.Parse(context.Request["Index"]);  //读取参数
                string paraWhere = context.Request["Where"] == null ? "" : context.Request["Where"].ToString();  //读取参数
                string paraClassify = context.Request["Classify"] == null ? "" : context.Request["Classify"].ToString();  //读取参数
                ImgInfo imgInfo = new ImgInfo();
                if (paraClassify == "single")
                {
                    imgInfo.Photo = DataSourceForImgRepeater<Model.Photo>(paraIndex, paraClassify, paraWhere);
                    imgInfo.Album = null;
                }
                else if (paraClassify == "album")
                {
                    imgInfo.Album = DataSourceForImgRepeater<Model.Album>(paraIndex, paraClassify, paraWhere);
                    imgInfo.Photo = null;
                }
                imgInfo.TotalNum = ImgsNum(imgInfo);
                imgInfo.Classify = paraClassify;

                context.Response.Write(JsonHelper.Serialize(imgInfo));
            }
            if (fun_id != null && fun_id.ToString() == "BTL")
            {
                context.Response.Write(JsonHelper.Serialize(BindTagList()));
            }
        }

        /// <summary>
        /// 返回当前页码对应的数据
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="where">选择条件</param>
        /// <returns></returns>
        public static List<T> DataSourceForImgRepeater<T>(int index,string classify, string where)
        {
            Token.Piker.BLL.AlbumBLL albumBll = new Token.Piker.BLL.AlbumBLL();
            Token.Piker.BLL.ViewPhotoTagBLL VsingleBll = new Token.Piker.BLL.ViewPhotoTagBLL();
            //绑定图片数据
            System.Data.DataSet DataSource = new DataSet();
            if (classify == "single")
            {
                DataSource = VsingleBll.GetListByPage(where, "ID", (index - 1) * pageSize + 1, index * pageSize);
            }
            else if (classify == "album")
            {
                DataSource = albumBll.GetListByTagName(where, "ID", (index - 1) * pageSize + 1, index * pageSize);
            }
            return DataSetToIList<T>(DataSource,0);
        }

        /// <summary>
        /// 返回当前图片的总数
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="where">选择条件</param>
        /// <returns></returns>
        public int ImgsNum(ImgInfo imgInfo)
        {
            //绑定图片数据
            int num = 0;
            if (imgInfo.Photo != null)
            {
                num = imgInfo.Photo.Count;
            }
            else if (imgInfo.Album != null)
            {
                num = imgInfo.Album.Count;
            }
            return num;
        }

        /// <summary>
        /// 绑定标签内容
        /// </summary>
        public List<Model.Tag> BindTagList()
        {
            Piker.BLL.TagBLL tagBll = new BLL.TagBLL();
            //绑定内容标签
            DataSet contentTags = tagBll.GetList("");
            return DataSetToIList<Model.Tag>(contentTags,0);
        }

        /// <summary>   
        /// DataSet装换为泛型集合   
        /// </summary>   
        /// <typeparam name="T"></typeparam>   
        /// <param name="p_DataSet">DataSet</param>   
        /// <param name="p_TableIndex">待转换数据表索引</param>   
        /// <returns></returns>   
        /// 2008-08-01 22:46 HPDV2806   
        public static List<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
        {
            if (p_DataSet == null || p_DataSet.Tables.Count < 0)
                return null;
            if (p_TableIndex > p_DataSet.Tables.Count - 1)
                return null;
            if (p_TableIndex < 0)
                p_TableIndex = 0;

            DataTable p_Data = p_DataSet.Tables[p_TableIndex];
            // 返回值初始化   
            List<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                System.Reflection.PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {
                        // 属性与字段名称一致的进行赋值   
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理   
                            if (p_Data.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                result.Add(_t);
            }
            return result;
        }
    }
}